﻿using Acme.BookStore.GetRoomInfo;
using Acme.BookStore.Goodss;
using Acme.BookStore.GoodTable;
using Acme.BookStore.OrderTable;
using Acme.BookStore.RoomNumbers;
using Acme.BookStore.RoomTable;
using HIS;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Acme.BookStore.Orders
{
    [ApiExplorerSettings(GroupName = "v1")]
    public class OrderSevices : ApplicationService, IOrdersServices
    {
        private readonly IRepository<Bookroom> orderrepository;
        private readonly IRepository<RoomType> roomTyperepository;

        public OrderSevices(IRepository<Bookroom> orderrepository, IRepository<RoomType> roomTyperepository)
        {
            this.orderrepository = orderrepository;
            this.roomTyperepository = roomTyperepository;
        }



        /// <summary>
        /// 订单添加
        /// </summary>
        /// <param name="OrderCodeInfoeDTO"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost("api/OrderAdd")]
        public async Task<APIResult> OrdersAdd(OrdersCodeDTO OrdersCodeDTO)
        {
            try
            {
                Bookroom bookCodeInfo = ObjectMapper.Map<OrdersCodeDTO, Bookroom>(OrdersCodeDTO);
                await orderrepository.InsertAsync(bookCodeInfo);
                if (bookCodeInfo == null)
                {
                    return new APIResult()
                    {
                        Code = CodeEnum.error,
                        Message = "添加失败"
                    };
                }
                return new APIResult()
                {
                    Code = CodeEnum.success,
                    Message = "酒店预定成功"
                };
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 删除订单
        /// </summary>
        /// <param name="Ids"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpDelete("api/orderDelete")]
        public async Task<APIResult> OrdersInfoDelete(string Ids)
        {
            try
            {
                var idList = Ids.Split(',').Select(x => Convert.ToInt32(x)).ToList();
                var orderTypes = await orderrepository.GetListAsync(x => idList.Contains(x.Id));

                if (orderTypes == null || !orderTypes.Any())
                {
                    return new APIResult()
                    {
                        Code = CodeEnum.error,
                        Message = "删除失败，未找到指定的商品"
                    };
                }

                foreach (var roomType in orderTypes)
                {
                    await orderrepository.DeleteAsync(roomType);
                }

                return new APIResult()
                {
                    Code = CodeEnum.success,
                    Message = "删除成功"
                };
            }
            catch (Exception ex)
            {
                return new APIResult()
                {
                    Code = CodeEnum.error,
                    Message = $"删除失败: {ex.Message}"
                };
            }
        }
        /// <summary>
        /// 订单展示
        /// </summary>
        /// <param name="BookName"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet("api/Orderinfo")]
        public async Task<APIResult<List<GetOrderInfoDTO>>> OrdersInfoShow(string BookName)
        {
            APIResult<List< GetOrderInfoDTO>> result = new APIResult<List<GetOrderInfoDTO>>();
            result.Code = CodeEnum.success;
            result.Message = CodeEnum.success.ToString();
            try
            {
                var list= from a in await orderrepository.GetListAsync()
                          join b in await roomTyperepository.GetListAsync() on a.RoomCode equals b.Id
                          where a.BookName==BookName
                          select new GetOrderInfoDTO
                          {
                              RoomCode = a.RoomCode,
                              BookName = a.BookName,
                              BookPhone = a.BookPhone,
                              RzTime = a.RzTime,
                              LdTime = a.LdTime,
                              Money = a.Money,
                              OrderState = a.OrderState,
                              RoomName = b.RoomName,
                              RoomPrice = b.RoomPrice,
                              RoomDeposit = b.RoomDeposit,
                              RoomAcreage = b.RoomAcreage,
                              PeopleNumber = b.PeopleNumber,
                              Partner = b.Partner,
                              AddBed = b.AddBed,
                              RoomTypeImage = b.RoomTypeImage,
                              RoomTypeVideo = b.RoomTypeVideo,
                              RoomTypeVR = b.RoomTypeVR,
                              RoomTypeDesc = b.RoomTypeDesc,
                          };
                result.Data = list.ToList();
            }
            catch (Exception e)
            {
                result.Code = CodeEnum.error;
                result.Message = CodeEnum.error.ToString() + e.Message;
                //throw;
            }
            return await Task.FromResult(result);
        }
        /// <summary>
        /// 商品更新
        /// </summary>
        /// 
        /// <param name="OrderCodeInfoUpdateDTO"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>        
        [HttpPut("api/orderput")]
        public async Task<APIResult> OrdersUpdate(OrdersServicesUpdateDTO OrderCodeInfoUpdateDTO)
        {
            try
            {
                var orderType = await orderrepository.FirstOrDefaultAsync(x => x.Id == OrderCodeInfoUpdateDTO.Id);
                if (orderType == null)
                {
                    return new APIResult()
                    {
                        Code = CodeEnum.error,      
                        Message = "商品未找到"
                    };
                }

                ObjectMapper.Map(OrderCodeInfoUpdateDTO, orderType);
                await orderrepository.UpdateAsync(orderType);

                 return new APIResult()
                {
                    Code = CodeEnum.success,
                    Message = "商品修改成功"
                };
            }
            catch (Exception ex)
            {
                return new APIResult()
                {
                    Code = CodeEnum.error,
                    Message = $"修改失败: {ex.Message}"
                };
            }
        }
    }
}
